package network;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

public class Packet_Update extends Packet{
//packet update from client to server
	public byte []m_record;
	
	public Packet_Update(int timestamp, byte [] record)
	{
		m_timestamp=timestamp;
		m_record=record;
		m_type=packettype.UPDATE;
	}
	
	public Packet_Update(DatagramPacket pack)
	{
		m_address=pack.getAddress();
		ByteBuffer buffer=ByteBuffer.wrap(pack.getData());
		Header(pack.getAddress(),buffer);		
		//read instance		
		m_record=new byte[12];
		for (int i=0;i<12;i++)
		{
			m_record[i]=buffer.get();
			
		}	
		
	}
	
	public DatagramPacket Encode(InetAddress address, int port)
	{
		byte []buffer=new byte[1024];
		ByteBuffer BB=ByteBuffer.wrap(buffer);		
		//encode type
		BB.put(getTypebyte(m_type));
		BB.putInt(m_timestamp);			
		for (int i=0;i<12;i++)
		{

			
			BB.put(m_record[i]);
		}
		
		return new DatagramPacket(buffer,buffer.length,address,port);
	}
	
}
